﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.Pages.Bus
{
    public partial class Region : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1208) == true)
                {
                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                        gvRegionBind();

                    }
                    lbltest.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }
        #region Region
        private void gvRegionBind()
        {
            gvRegion.DataSource = School.Service.Bus.GetAllDistrict();
            gvRegion.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool re = School.Service.Bus.AddRegion(Convert.ToInt32(ddCamp.SelectedValue.ToString()), txtcode.Text, txtName.Text);

            if (re == true)
            {
                lbltest.Text = "تمت الاضافة بنجاح";
                ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                gvRegionBind();
            }
            else
            {
                lbltest.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltest.Visible = true;
        }

        protected void gvRegion_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvRegion.EditIndex = e.NewEditIndex;
            gvRegionBind();
        }

        protected void gvRegion_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRegion.EditIndex = -1;
            gvRegionBind();
        }

        protected void gvRegion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvRegion.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Service.Bus.DeleteDistrict(Id);
            if (re == true)
            {
                lbltest.Text = "تم الحذف بنجاح";
                gvRegionBind();
            }
            else
            {
                lbltest.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltest.Visible = true;
        }

        protected void gvRegion_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvRegion.DataKeys[e.RowIndex].Value.ToString());
            TextBox Name = (TextBox)gvRegion.Rows[e.RowIndex].FindControl("txtN");
            TextBox Code = (TextBox)gvRegion.Rows[e.RowIndex].FindControl("txtC");
            bool re = School.Service.Bus.UpdateDistrict(Id, Code.Text, Name.Text);
            if (re == true)
            {
                lbltest.Text = "تم التحديث بنجاح";
                gvRegion.EditIndex = -1;
                gvRegionBind();
            }
            else
            {
                lbltest.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltest.Visible = true;
        }
        protected void gvRegion_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRegion.PageIndex = e.NewPageIndex;
            gvRegionBind();
        }
        #endregion
    }
}